package com.cx.controller;

import com.cx.common.ApiReturn;
import com.cx.pojo.Series;
import com.cx.service.SeriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("series")
public class SeriesController {

    @Autowired
    SeriesService seriesService;

    //添加车系
    @RequestMapping("insertSeries")
    @ResponseBody//将数据转换成json后返回给前端
    public ApiReturn insertSeries(Series series){
        int rows = seriesService.insert(series);
        if (rows<1){
            return ApiReturn.error("添加车系失败");
        }
        return ApiReturn.success("添加车系成功");
    }

    //修改车系
    @RequestMapping("updateSeries")
    @ResponseBody
    public ApiReturn updateSeries(Series series) {
        int rows = seriesService.updateByPrimaryKey(series);
        if (rows < 1) {
            return ApiReturn.error("修改车系失败");
        }
        return ApiReturn.success("修改车系成功");
    }

    //删除车系
    @RequestMapping("deleteSeries")
    @ResponseBody
    public ApiReturn deleteSeries(Integer seriesId) {
        int rows = seriesService.deleteByPrimaryKey(seriesId);
        if (rows < 1) {
            return ApiReturn.error("删除车系失败");
        }
        return ApiReturn.success("删除车系成功");
    }

    //根据seriesId查询汽车
    @RequestMapping("selectSeriesById")
    @ResponseBody
    public ApiReturn selectSeriesById(Integer seriesId) {
        Series seriesInfo = seriesService.selectByPrimaryKey(seriesId);
        if (seriesInfo != null) {
            return ApiReturn.success("品牌数据",seriesInfo);
        }
        return ApiReturn.error("未获取该品牌数据");
    }

    //查询所有汽车车系
    @RequestMapping("selectAllSeries")
    @ResponseBody
    public ApiReturn selectAllSeries(Series series) {
        List<Series> seriesList = seriesService.selectAll(series);
        if (seriesList !=null){
            return ApiReturn.success("返回车系数据",seriesList);
        }
        return ApiReturn.error("无数据");
    }

    //根据车系名查询汽车
    @RequestMapping("selectSeries")
    @ResponseBody
    public ApiReturn selectSeries(String seriesName) {
        Series series = seriesService.selectSeries(seriesName);
        if (series != null) {
            return ApiReturn.success("返回该品牌",series);
        }
        return ApiReturn.error("未获取该品牌数据");
    }
}
